package com.yanqu.road.dao.impl.activity.rankunionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.rankunionwar.data.*;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class CrossRankUnionWarMatchDataDaoImpl extends TempDao {

    public long getMaxBarrageNo(int activityId) {
        String sql = "select max(barrageNo) as maxBarrageNo from t_c_activity_rank_union_war_barrage where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                long data = rs.getLong("maxBarrageNo");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    public int getMaxPatronsNo(int activityId) {
        String sql = "select max(patronsNo) as maxPatronsNo from t_c_activity_rank_union_war_bpatrons where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                int data = rs.getInt("maxPatronsNo");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    public int getMaxGameNo(int activityId) {
        String sql = "select max(gameNo) as maxGameNo from t_c_activity_rank_union_war_match where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                int data = rs.getInt("maxGameNo");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    //比赛
    public  List<CrossRankUnionWarMatchData> getMatchList(int activityId) {
        List<CrossRankUnionWarMatchData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_rank_union_war_match WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarMatchData data = initMatch(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    protected  CrossRankUnionWarMatchData initMatch(ResultSet rs) throws SQLException {
        CrossRankUnionWarMatchData data = new  CrossRankUnionWarMatchData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGameNo(rs.getInt("gameNo"));
        data.setTurn(rs.getInt("turn"));
        data.setDivision(rs.getInt("division"));
        data.setStatus(rs.getInt("status"));
        data.setWinUnionId(rs.getString("winUnionId"));
        data.setWinTime(rs.getLong("winTime"));
        data.setUnionId1(rs.getString("unionId1"));
        data.setUnionId2(rs.getString("unionId2"));
        data.setMorale1(rs.getInt("morale1"));
        data.setMorale2(rs.getInt("morale2"));
        data.setAttackUnionId(rs.getString("attackUnionId"));
        data.setUnion1Support(rs.getInt("union1Support"));
        data.setUnion2Support(rs.getInt("union2Support"));
        data.setBattleTime(rs.getLong("battleTime"));
        data.setNo(rs.getInt("no"));

        String userInspireTimesDataStr  = rs.getString("userInspireTimesData");
        if(!StringUtils.isNullOrEmpty(userInspireTimesDataStr)){
            Map<Long, Integer> result = JSON.parseObject(userInspireTimesDataStr, new TypeReference<Map<Long, Integer>>() {});
            if(result != null){
                data.setUserInspireTimesMap(result);
            }
        }


        String killRankDataStr  = rs.getString("killRankData");
        if(!StringUtils.isNullOrEmpty(killRankDataStr)){
            List<CrossRankUnionWarKillRankData> result = JSON.parseObject(killRankDataStr, new TypeReference<List<CrossRankUnionWarKillRankData>>() {});
            if(result != null){
                data.setKillRankList(result);
                Map<Long, CrossRankUnionWarKillRankData> killRankMap = new ConcurrentHashMap<>();
                for(CrossRankUnionWarKillRankData tempData : result){
                    killRankMap.put(tempData.getUserId(),tempData);
                }
                data.setKillRankMap(killRankMap);
            }
        }

        String noticeDataStr  = rs.getString("noticeData");
        if(!StringUtils.isNullOrEmpty(noticeDataStr)){
            List<CrossRankUnionWarNoticeData> result = JSON.parseObject(noticeDataStr, new TypeReference<List<CrossRankUnionWarNoticeData>>() {});
            if(result != null){
                data.setNoticeList(result);
            }
        }

        String lookRewardDataStr  = rs.getString("lookRewardData");
        if(!StringUtils.isNullOrEmpty(lookRewardDataStr)){
            List<Long> result = JSON.parseObject(lookRewardDataStr, new TypeReference<List<Long>>() {});
            if(result != null){
                data.setLookRewardList(result);
            }
        }

        String matchRewardDataStr  = rs.getString("matchRewardData");
        if(!StringUtils.isNullOrEmpty(matchRewardDataStr)){
            List<Long> result = JSON.parseObject(matchRewardDataStr, new TypeReference<List<Long>>() {});
            if(result != null){
                data.setMatchRewardList(result);
            }
        }

        return data;
    }

    public boolean addMatchList(List<CrossRankUnionWarMatchData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_match(" +
                " activityId,gameNo,turn,division,status,winUnionId,winTime,unionId1,unionId2,morale1,morale2,attackUnionId,userInspireTimesData," +
                " union1Support,union2Support,killRankData,noticeData,lookRewardData,matchRewardData,battleTime,no) " +
                " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarMatchData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getTurn()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getDivision()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getStatus()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getWinUnionId()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getWinTime()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId1()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId2()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getMorale1()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getMorale2()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getAttackUnionId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getUserInspireTimesMap())));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getUnion1Support()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getUnion2Support()));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getKillRankList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getNoticeList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getLookRewardList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getMatchRewardList())));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getBattleTime()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getNo()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean updateMatchList(List<CrossRankUnionWarMatchData> dataList) {
        String sql = "UPDATE t_c_activity_rank_union_war_match SET " +
                " turn=?,division=?,status=?,winUnionId=?,winTime=?,unionId1=?,unionId2=?,morale1=?,morale2=?,attackUnionId=?,userInspireTimesData=?," +
                " union1Support=?,union2Support=?,killRankData=?,noticeData=?,lookRewardData=?,matchRewardData=?,battleTime=?" +
                " WHERE activityId=? and gameNo = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarMatchData  data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getTurn()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getDivision()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getStatus()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getWinUnionId()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getWinTime()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId1()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId2()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getMorale1()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getMorale2()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getAttackUnionId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getUserInspireTimesMap())));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getUnion1Support()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getUnion2Support()));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getKillRankList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getNoticeList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getLookRewardList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getMatchRewardList())));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getBattleTime()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }


    //每条线路
    public  List<CrossRankUnionWarLineData> getMatchLineList(int activityId,int gameNo) {
        List<CrossRankUnionWarLineData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_rank_union_war_line WHERE activityId = ? AND gameNo = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,gameNo);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarLineData data = initMatchLine(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    protected  CrossRankUnionWarLineData initMatchLine(ResultSet rs) throws SQLException {
        CrossRankUnionWarLineData data = new  CrossRankUnionWarLineData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGameNo(rs.getInt("gameNo"));
        data.setLine(rs.getInt("line"));
        data.setLineIndex1(new AtomicInteger(rs.getInt("lineIndex1")));
        data.setLineIndex2(new AtomicInteger(rs.getInt("lineIndex2")));
        data.setUnion1WaitTime(rs.getLong("union1WaitTime"));
        data.setUnion2WaitTime(rs.getLong("union2WaitTime"));
        data.setLastBattleTime(rs.getLong("lastBattleTime"));
        data.setUnion1StartWaitTime(rs.getLong("union1StartWaitTime"));
        data.setUnion2StartWaitTime(rs.getLong("union2StartWaitTime"));
        data.setWinUnionId(rs.getString("winUnionId"));
        data.setWinTime(rs.getLong("winTime"));
        data.setUnionId1(rs.getString("unionId1"));
        data.setUnionId2(rs.getString("unionId2"));

        return data;
    }

    public boolean addLineList(List<CrossRankUnionWarLineData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_line(" +
                " activityId,gameNo,line,lineIndex1,lineIndex2,union1WaitTime,union2WaitTime," +
                " lastBattleTime,union1StartWaitTime,union2StartWaitTime,winUnionId,winTime,unionId1,unionId2) " +
                " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarLineData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLine()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex1()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex2()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion1WaitTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion2WaitTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getLastBattleTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion1StartWaitTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion2StartWaitTime()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getWinUnionId()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getWinTime()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId1()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId2()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean updateMatchLineList(List<CrossRankUnionWarLineData> dataList) {
        String sql = "UPDATE t_c_activity_rank_union_war_line SET " +
                " lineIndex1=?,lineIndex2=?,union1WaitTime=?,union2WaitTime=?,lastBattleTime=?,union1StartWaitTime=?," +
                " union2StartWaitTime=?,winUnionId=?,winTime=?,unionId1=?,unionId2=?" +
                " WHERE activityId=? and gameNo = ? and line = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarLineData  data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex1()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex2()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion1WaitTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion2WaitTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getLastBattleTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion1StartWaitTime()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUnion2StartWaitTime()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getWinUnionId()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getWinTime()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId1()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionId2()));

                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLine()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }


    //等待门客
    public  List<CrossRankUnionWarWaitPatronData> getWaitPatronsList(int activityId,int gameNo) {
        List<CrossRankUnionWarWaitPatronData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_rank_union_war_wpatrons WHERE activityId = ? AND gameNo = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,gameNo);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarWaitPatronData data = initWaitPatrons(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    protected  CrossRankUnionWarWaitPatronData initWaitPatrons(ResultSet rs) throws SQLException {
        CrossRankUnionWarWaitPatronData data = new  CrossRankUnionWarWaitPatronData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGameNo(rs.getInt("gameNo"));
        data.setUserId(rs.getLong("userId"));
        data.setPatronId(rs.getInt("patronId"));
        data.setSkinId(rs.getInt("skinId"));
        data.setPromotionId(rs.getInt("promotionId"));
        data.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
        data.setLine(rs.getInt("line"));
        data.setLineIndex(rs.getInt("lineIndex"));
        data.setSendTime(rs.getLong("sendTime"));
        data.setBattleTimes(rs.getInt("battleTimes"));
        return data;
    }

    public boolean addWaitPatronsList(List<CrossRankUnionWarWaitPatronData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_wpatrons(" +
                " activityId,gameNo,userId,patronId,skinId,promotionId,earnSpeed,line,lineIndex,sendTime,battleTimes) " +
                " values (?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarWaitPatronData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getSkinId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPromotionId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getEarnSpeed().toString()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLine()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getSendTime()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getBattleTimes()));

                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean updateWaitPatronsList(List<CrossRankUnionWarWaitPatronData> dataList) {
        String sql = "UPDATE t_c_activity_rank_union_war_wpatrons SET " +
                " skinId=?,promotionId=?,earnSpeed=?,line=?,lineIndex=?,sendTime=?,battleTimes=?" +
                " WHERE activityId=? and gameNo = ? and userId = ? and patronId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarWaitPatronData  data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getSkinId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPromotionId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getEarnSpeed().toString()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLine()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getSendTime()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getBattleTimes()));

                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronId()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean delWaitPatronsList(List<CrossRankUnionWarWaitPatronData> dataList) {
        String sql = "DELETE FROM t_c_activity_rank_union_war_wpatrons " +
                " WHERE activityId=? and gameNo = ? and userId = ? and patronId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarWaitPatronData  data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronId()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    //作战门客
    public  List<CrossRankUnionWarBattlePatronData> getBattlePatronsList(int activityId,int gameNo) {
        List<CrossRankUnionWarBattlePatronData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_rank_union_war_bpatrons WHERE activityId = ? AND gameNo = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,gameNo);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarBattlePatronData data = initBattlePatrons(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    protected  CrossRankUnionWarBattlePatronData initBattlePatrons(ResultSet rs) throws SQLException {
        CrossRankUnionWarBattlePatronData data = new  CrossRankUnionWarBattlePatronData();
        data.setActivityId(rs.getInt("activityId"));
        data.setPatronsNo(rs.getInt("patronsNo"));
        data.setGameNo(rs.getInt("gameNo"));
        data.setUserId(rs.getLong("userId"));
        data.setPatronId(rs.getInt("patronId"));
        data.setSkinId(rs.getInt("skinId"));
        data.setPromotionId(rs.getInt("promotionId"));
        data.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
        data.setBool(new BigInteger(rs.getString("bool")));
        data.setLine(rs.getInt("line"));
        data.setLineIndex(rs.getInt("lineIndex"));
        data.setSendTime(rs.getLong("sendTime"));
        data.setKillTimes(rs.getInt("killTimes"));
        data.setDie(rs.getBoolean("die"));
        return data;
    }

    public boolean addBattlePatronsList(List<CrossRankUnionWarBattlePatronData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_bpatrons(" +
                " activityId,patronsNo,gameNo,userId,patronId,skinId,promotionId,earnSpeed,bool,line,lineIndex,sendTime,killTimes,die) " +
                " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarBattlePatronData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronsNo()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getSkinId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPromotionId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getEarnSpeed().toString()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getBool().toString()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLine()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getSendTime()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getKillTimes()));
                para.put(i++, new DbParameter(Types.BOOLEAN,  data.isDie()));

                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean updateBattlePatronsList(List<CrossRankUnionWarBattlePatronData> dataList) {
        String sql = "UPDATE t_c_activity_rank_union_war_bpatrons SET " +
                " gameNo=?,userId=?,patronId=?,skinId=?,promotionId=?,earnSpeed=?,bool=?,line=?,lineIndex=?,sendTime=?,killTimes=?,die=?" +
                " WHERE activityId=? and patronsNo = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarBattlePatronData  data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getSkinId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPromotionId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getEarnSpeed().toString()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getBool().toString()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLine()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getLineIndex()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getSendTime()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getKillTimes()));
                para.put(i++, new DbParameter(Types.BOOLEAN,  data.isDie()));

                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getPatronsNo()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }


    //竞猜
    public  List<CrossRankUnionWarGambleData> getGambleList(int activityId) {
        List<CrossRankUnionWarGambleData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_rank_union_war_gamble WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarGambleData data = initGamble(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    protected  CrossRankUnionWarGambleData initGamble(ResultSet rs) throws SQLException {
        CrossRankUnionWarGambleData data = new  CrossRankUnionWarGambleData();
        data.setActivityId(rs.getInt("activityId"));
        data.setTurn(rs.getInt("turn"));
        data.setOver(rs.getBoolean("over"));

        String gameNoDataStr  = rs.getString("gameNoData");
        if(!StringUtils.isNullOrEmpty(gameNoDataStr)){
            Map<Integer, List<String>> result = JSON.parseObject(gameNoDataStr, new TypeReference<Map<Integer, List<String>>>() {});
            if(result != null){
                data.setGameNoMap(result);
            }
        }


        String winUnionIdDataStr  = rs.getString("winUnionIdData");
        if(!StringUtils.isNullOrEmpty(winUnionIdDataStr)){
            List<String> result = JSON.parseObject(winUnionIdDataStr, new TypeReference<List<String>>() {});
            if(result != null){
                data.setWinUnionIdList(result);
            }
        }

        String guessUnionIdDataStr  = rs.getString("guessUnionIdData");
        if(!StringUtils.isNullOrEmpty(guessUnionIdDataStr)){
            Map<Long, Map<Integer, String>> result = JSON.parseObject(guessUnionIdDataStr, new TypeReference<Map<Long, Map<Integer, String>>>() {});
            if(result != null){
                data.setGuessUnionIdMap(result);
            }
        }

        return data;
    }

    public boolean addGambleList(List<CrossRankUnionWarGambleData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_gamble(" +
                " activityId,turn,gameNoData,winUnionIdData,guessUnionIdData,over) " +
                " values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarGambleData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getTurn()));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getGameNoMap())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getWinUnionIdList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getGuessUnionIdMap())));
                para.put(i++, new DbParameter(Types.BOOLEAN,  data.isOver()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean updateGambleList(List<CrossRankUnionWarGambleData> dataList) {
        String sql = "UPDATE t_c_activity_rank_union_war_gamble SET " +
                " gameNoData=?,winUnionIdData=?,guessUnionIdData=?,over=?" +
                " WHERE activityId=? and turn = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarGambleData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getGameNoMap())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getWinUnionIdList())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getGuessUnionIdMap())));
                para.put(i++, new DbParameter(Types.BOOLEAN,  data.isOver()));

                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getTurn()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    //珍兽和鱼的锁
    public  Map<Long,CrossRankUnionWarUserLockData> getUserLockList(int activityId) {
        Map<Long,CrossRankUnionWarUserLockData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_rank_union_war_lock WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarUserLockData data = initUserLock(rs);
                dataMap.put(data.getUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    protected  CrossRankUnionWarUserLockData initUserLock(ResultSet rs) throws SQLException {
        CrossRankUnionWarUserLockData data = new  CrossRankUnionWarUserLockData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setUserId(rs.getLong("userId"));

        String vehicleDataStr  = rs.getString("vehicleData");
        if(!StringUtils.isNullOrEmpty(vehicleDataStr)){
            Map<String, Integer> result = JSON.parseObject(vehicleDataStr, new TypeReference<ConcurrentHashMap<String, Integer>>() {});
            if(result != null){
                data.setVehiclePatronsMap(result);
            }
        }


        String fishDataStr  = rs.getString("fishData");
        if(!StringUtils.isNullOrEmpty(fishDataStr)){
            Map<Integer, Integer> result = JSON.parseObject(fishDataStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                data.setFishPatronsMap(result);
            }
        }

        String cricketDataStr  = rs.getString("cricketData");
        if(!StringUtils.isNullOrEmpty(cricketDataStr)){
            Map<Integer, Integer> result = JSON.parseObject(cricketDataStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                data.setCricketPatronsMap(result);
            }
        }

        return data;
    }

    public boolean addUserLockList(List<CrossRankUnionWarUserLockData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_lock(" +
                " activityId,unionUid,userId,vehicleData,fishData,cricketData) " +
                " values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarUserLockData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getUnionUid()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getVehiclePatronsMap())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getFishPatronsMap())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getCricketPatronsMap())));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public boolean updateUserLockList(List<CrossRankUnionWarUserLockData> dataList) {
        String sql = "UPDATE t_c_activity_rank_union_war_lock SET " +
                " vehicleData=?,fishData=?,cricketData=?" +
                " WHERE activityId=? and userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarUserLockData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getVehiclePatronsMap())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getFishPatronsMap())));
                para.put(i++, new DbParameter(Types.VARCHAR,  JSONArray.toJSONString(data.getCricketPatronsMap())));

                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }


    public boolean addBarrageList(List<CrossRankUnionWarBarrageData> dataList) {
        String sql = "insert into t_c_activity_rank_union_war_barrage(" +
                " activityId,barrageNo,gameNo,userId,content,time) " +
                " values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            for (CrossRankUnionWarBarrageData data : dataList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER,  data.getActivityId()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getBarrageNo()));
                para.put(i++, new DbParameter(Types.INTEGER,  data.getGameNo()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR,  data.getContent()));
                para.put(i++, new DbParameter(Types.BIGINT,  data.getTime()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }
}
